﻿
big_picture_control = {
    should_hide_other_wnd = false;

    init = function()
        big_picture_control.inner_log("init");
        ierd_tgp.tpf_ui.Require("big_picture\\model.lua");
        ierd_tgp.tpf_ui.Require("big_picture\\view.lua");
        ierd_tgp.tpf_ui.Require("big_picture\\popbox.lua");
    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[big_picture][control] %s", msg), true);
    end;
    
    show_bp_wnd = function(data)
        big_picture_control.inner_log("show_bp_wnd : "..base_utility.table_to_json(data));
        if data == nil or data.show == nil then
            return;
        end
        if data.show == 1 then
            big_picture_view.open_big_picture_wnd();
        else
            big_picture_view.close_big_picture_wnd();
        end
    end;

    active_bp_wnd = function()
        if base_utility.is_big_picture_ui() == false then
            return;
        end;
        big_picture_view.active_wnd();
    end;

    on_game_start = function()
        --[[if base_utility.is_big_picture_ui() then
            big_picture_view.close_big_picture_wnd();
        end--]]
        local data = {
            msg_type = "notify_game_start_or_end",
            req_param = "1"
        };
        comm_center.call_service("i_service_bp_ui_request", data);
    end;

    on_game_end = function()
        --[[if base_utility.is_big_picture_ui() then
            big_picture_view.open_big_picture_wnd();
        end--]]
        local data = {
            msg_type = "notify_game_start_or_end",
            req_param = "0"
        };
        comm_center.call_service("i_service_bp_ui_request", data);
    end;

    notify_browser_msg = function(data)
        big_picture_view.notify_browser_msg(data);
    end;

    on_bp_call_ui = function(data)
        big_picture_control.inner_log("on_bp_call_ui : "..base_utility.table_to_json(data));
        if data == nil or data.msg_type == nil then
            return;
        end

        if data.msg_type == "bp_start_game" and data.req_param ~= nil then
            local param = base_utility.json_to_table(data.req_param);
            if param ~= nil and param.game_id ~= nil then
                local game_id = tonumber(param.game_id);
                local status = banner_model.get_game_state(game_id);
                if status == nil or status.iState == nil or 
                   status.iState ~= ierd_tgp.game_library.GAME_STATE_NORMAL then
                    local game_detail_info = banner_model.get_game_base_info(game_id);
                    local game_name = "";
                    if game_detail_info ~= nil and game_detail_info.name ~= nil then
                        game_name = game_detail_info.name;
                    end
                    ui_utility.show_bp_launch_error_tips(
                        game_id, game_name, 0, LoadStr("TOOL_BOX_LOAD_PAGE_NOT_OPEN_GAME"));
                    return;
                end

                local data_in = {};
                data_in.game_id = game_id;

                data_in.server_id = 0;
                local game_servers = banner_model.get_game_servers(game_id);
                if game_servers ~= nil then
                    data_in.server_id = game_servers.selleted;
                end
                
                local data_out = comm_center.call_service('i_service_start_game', data_in);
                big_picture_control.report("start_game", base_utility.table_to_json(data_in));
            end
        elseif data.msg_type == "bp_start_cloud_game" and data.req_param ~= nil then
            big_picture_control.on_start_cloud_game(data.req_param);
        elseif data.msg_type == "bp_open_assist_setting" and data.req_param ~= nil then
            big_picture_control.on_open_assist_setting(data.req_param);
        elseif data.msg_type == "bp_minimize_wnd" then
            big_picture_control.report("minimize_wnd", "");
            big_picture_view.mini_dlg();
        elseif data.msg_type == "bp_exit_bp_mode" then
            big_picture_control.report("exit_bp_mode", "");
            big_picture_view.exit_mode();
        elseif data.msg_type == "bp_exit_wegame" then
            big_picture_control.report("exit_wegame", "");
            big_picture_view.exit_wegame();
        elseif data.msg_type == "active_bp_wnd" then
            big_picture_control.active_bp_wnd();
        elseif data.msg_type == "check_bp_wnd_ready" then
            if big_picture_view.is_bp_wnd_ready() then
                data.rsp_param = "1";
            else
                data.rsp_param = "0";
            end
            return data;
        elseif data.msg_type == "check_game_protocol_agree_info" then
            if nil == frame_bottom_model.is_user_protocol_agree_table then
                frame_bottom_model.get_game_protocol_agree_info();
            end

            local param = base_utility.json_to_table(data.req_param);
            if param ~= nil and param.game_id ~= nil then
                local game_id = tonumber(param.game_id);
                local is_agree = frame_bottom_model.is_all_protocol_agree(game_id);
                if is_agree then
                    data.rsp_param = "1";
                else
                    data.rsp_param = "0";
                end
                return data;
            end
        elseif data.msg_type == "show_mask" then
            data.rsp_param = "0";
            local show = false;
            if data.req_param == "1" then
                show = true;
            end
            if big_picture_view.show_mask(show) == true then
                data.rsp_param = "1";
            end;
            return data;
        end
    end;

    on_ui_mode_change = function(data)
        big_picture_control.inner_log("on_ui_mode_change : "..base_utility.table_to_json(data));
        if data ~= nil and data.normal_ui ~= nil and data.bigpic_ui ~= nil then
            big_picture_control.report("ui_mode_change", base_utility.table_to_json(data));

            big_picture_model.ui_mode_ = data.mode;
            if data.normal_ui == 1 then
                -- 拉起主界面
                event_center.send_event(mainframe_event.SHOW_MAIN_WND, "");
                -- 若不在游戏模式，重启任务
                local info = comm_center.call_service("Svr_GetRunMode", {is_game_mode = false});
                if info ~= nil and info.is_game_mode ~= nil and info.is_game_mode == false then
                    comm_center.call_service("i_service_gamedl_mgr_start", {game_id = 0});
                end
            elseif data.bigpic_ui == 1 then
                -- 暂停所有的下载任务
                comm_center.call_service("i_service_gamedl_mgr_pause", {game_id = 0});
                big_picture_control.should_hide_other_wnd = true;
            end
        end
    end;

    on_streaming_mode_change = function(data)
        if data ~= nil and data.mode ~= nil then
            big_picture_control.report("streaming_mode_change", base_utility.table_to_json(data));
        end;
    end;

    on_bp_wnd_shown = function()
        if base_utility.is_big_picture_ui() and big_picture_control.should_hide_other_wnd then
            -- 关闭主界面
            --wegame_mainframe_view.close_main_window();
            -- 关闭其他弹窗
            ui_utility.show_only_child(Tenio.ROOT_CONTROLID, "BigPictureDlg");
            big_picture_control.should_hide_other_wnd = false;
        end
    end;

    hide_all_dlg = function()
        -- 隐藏所有root下面的子窗口
        local pUILib = ui_utility.uilib();
        if nil == pUILib then
            return;
        end;

        local pRoot = pUILib:GetControlPtr(Tenio.ROOT_CONTROLID);
        if nil == pRoot then
            return;
        end

        local pChild = pRoot:GetFirstChild();

        while (nil ~= pChild) do
            local id = pChild:GetControlID();
            --shadow_helper.destroy_shadow(id);
            pChild:Show(FALSE);
            pChild = pChild:GetUpperSibling();
        end;

        event_center.send_event(tab_browser_event.CLOSE_TAB_BROWSER_WINDOW, "");
        event_center.send_event(browser_event.CLOSE_ALL_IND_WEB_WND, "");
    end;

    update_game_state = function(event_id, json_param)
        if big_picture_view.is_bp_wnd_ready() == false then
            return;
        end;

        local json_tbl = base_utility.json_to_table(json_param);
        if json_tbl.game_id == nil then
            return
        end
        local param = {game_id = tostring(json_tbl.game_id)};
        local data = {
            msg_type = "notify_update_bp_game_state",
            req_param = base_utility.table_to_json(param) 
        };
        comm_center.call_service("i_service_bp_ui_request", data);
    end;

    notify_bp_wnd_ready = function(is_ready)
        local ready = "0";
        if is_ready ~= nil and is_ready == true then
            ready = "1";
        end
        local data = {
            msg_type = "notify_bp_wnd_ready",
            req_param = ready 
        };
        comm_center.call_service("i_service_bp_ui_request", data);
    end;

    on_start_cloud_game = function(json_param)
        if json_param == nil then
            return;
        end;
        local param = base_utility.json_to_table(json_param);
        if param == nil or param.game_id == nil then
            return;
        end;

        local game_id = tonumber(param.game_id);

        local query_param = { game_id =game_id};
        local query_ret = comm_center.call_service("Svr_GameLibrary_QueryGameLaunchInfo", query_param);
        if query_ret == nil or query_ret.game_info_list == nil then
             base_utility.log("[bigpicture]on_start_cloud_game  error! no game info");
             return;
        end;
        local game_info = base_utility.json_to_table(query_ret.game_info_list);
        if game_info.has_cloud_game_authority == false then
           base_utility.log("[bigpicture] on_add_cloud_game_time");
           local purchase_param = {
           game_id = game_id;
           parameter = "#/?game_id=" .. tostring(game_id);
           width = 660;
           height = 500;
           };
           comm_center.call_service("SvrLua_CloudGame_Purchase_Request", purchase_param);
           return;
        end

        local launch_param = { game_id =game_id};
        comm_center.call_service("SvrLua_CloudGame_Launch_Request", launch_param);
    end;

    on_open_assist_setting = function(json_param)
        if json_param == nil then
            big_picture_control.inner_log("on_open_assist_setting, json_param nil", true);
            return;
        end;
        local param = base_utility.json_to_table(json_param);
        if param == nil or param.game_id == nil or tonumber(param.game_id) == 0 then
            big_picture_control.inner_log("on_open_assist_setting, param table or id empty", true);
            return;
        end;

        local game_id = tonumber(param.game_id);
        local game_info = banner_model.get_game_base_info(game_id);
        if nil == game_info then
            big_picture_control.inner_log("on_open_assist_setting, game base info nil", true);
            return;
        end
        if game_info.main_type == nil then
            big_picture_control.inner_log("on_open_assist_setting, game main_type nil", true);
            return;
        end;

        wegame_mainframe_control.only_focus_banner(game_id, "");

        if game_info.main_type == game_library_model.game_main_type.CONSOLE_GAME then
            plugin_tool_simple_panel.on_cross_setting_btn_click();
        else
            plugin_tool_panel.on_assist_setting_btn_click();
        end;
    end;

    on_joy_message = function(uilib, msg, wParam, lParam)
        base_utility.log(string.format("[big_picture][control] msg=%d, wParam=%d, lParam=%d", msg, wParam, lParam));
        local data = {
            source = 2,
            msg_type = msg,
            wparam = wParam,
            lparam = lParam
        };
        if big_picture_view.is_mask_visible() == false and 
           big_picture_view.is_big_pic_wnd_visible() == true and
           big_picture_control.no_cloud_game_running() == true and 
           big_picture_control.bp_wnd_is_foregroud() == true then
            comm_center.call_service("i_service_process_external_msg", data);
        end;
    end;

    report = function(type, json)
        local qos_data = ierd_tgp.tpf_ui.LuaQosDataEx:new();
        qos_data.oc_type    = 1;
        qos_data.qos_kind   = ierd_tgp.adapt_for_imports.qos.Qos_kind.kGameUpdateLackSpace;
        qos_data.str_action = "big_pic";
        qos_data.str_type   = type;
        qos_data.str_json   = json;
        base_utility.lua_qos_ex(qos_data);
        qos_data:delete();
    end;

    on_cloud_game_state_update = function(param)
        if param == nil or param.game_id == nil or param.state == nil then
            base_utility.log("on_cloud_game_state_update, param error", true);
            return;
        end;
        local cloud_game_state = param.state;
        if cloud_game_state == 4 then
            big_picture_model.running_cloud_game_id_ = tonumber(param.game_id);
        else
            if big_picture_model.running_cloud_game_id_ ~= nil and
               big_picture_model.running_cloud_game_id_ ~= 0 and
                big_picture_control.is_cloud_game_running(
                        big_picture_model.running_cloud_game_id_) == false then
                big_picture_model.running_cloud_game_id_ = 0;
            end;
        end;
    end;

    is_cloud_game_running = function(game_id)
        local ret = false;
        if game_id == nil or game_id == 0 then
            return ret;
        end;
        local data_out = comm_center.call_service("Svr_IsCloudGameRunning",
                                            {game_id = game_id});
        if data_out ~= nil and data_out.is_running then
            ret = true;
        end;
        return ret;
    end;

    no_cloud_game_running = function()
        local ret = true;
        if big_picture_model.running_cloud_game_id_ == nil or 
           big_picture_model.running_cloud_game_id_ == 0 then
            return ret;
        end;

        if big_picture_control.is_cloud_game_running(
                        big_picture_model.running_cloud_game_id_) == false then
            big_picture_model.running_cloud_game_id_ = 0;
        else
            ret = false;
        end;
        return ret;
    end;

    bp_wnd_is_foregroud = function()
        local ret = false;
        local data = {
            msg_type = "check_bp_wnd_is_foregroud",
            rsp_param = "0" 
        };
        local data_out = comm_center.call_service("i_service_bp_ui_request", data);
        if data_out.rsp_param == "1" then
            ret = true;
        end;
        return ret;
    end;
};

big_picture_control.init();
